
Imports cmplib01


Public Class frmIndicatorProperties
    Private ptrindicator As IIindciator
    Private _edit As Boolean = False
    Private _cancelled As Boolean = False
    Private bPopulating As Boolean

    Public Sub ShowDialogNewIndicator()
        _edit = False
        '// set up for edit
        grpValueIndicator.Visible = True
        cboIndicatorType.Enabled = True
        lblSelectIndicatorType.Visible = True
        txtName.ReadOnly = False
        bPopulating = False
        btnOK.Enabled = False

        MyBase.ShowDialog()
    End Sub

    Public ReadOnly Property Indi() As IIindciator
        Get
            Return ptrindicator
        End Get
    End Property

    Public Shadows Sub ShowDialog(ByRef indi As IIindciator)
        ptrindicator = indi
        '// this is an edit :D
        _edit = True

        '// to keep from "newing" the passed in indicator
        bPopulating = True

        '// populate
        Populate()
        lblSelectIndicatorType.Visible = False
        '// you can only CANCEL a new indicator, not the changes made to it... xxP
        btnOK.Location = btnCancel.Location
        btnCancel.Visible = False
        txtName.ReadOnly = True

        MyBase.ShowDialog()

    End Sub

    Private Sub Populate()
        Try

            txtName.Text = ptrindicator.IndicatorName

            Select Case ptrindicator.IndicatorType
                Case IndicatorType_enum.value
                    cboIndicatorType.Text = "Value Indicator"
                    grpValueIndicator.Visible = True
                    grpNameValueIndicator.Visible = False
                    grpNameIndicator.Visible = False
                    '// list the data stuff..
                    Dim vindicator As ValueIndicator = DirectCast(ptrindicator, ValueIndicator)
                    Populate(vindicator)

                Case IndicatorType_enum.named
                    cboIndicatorType.Text = "Name Indicator"

                    grpNameIndicator.Visible = True
                    grpNameIndicator.Location = grpValueIndicator.Location
                    grpNameValueIndicator.Visible = False
                    grpValueIndicator.Visible = False

                    '// list 'em!
                    Dim nindicator As NamedIndicator = DirectCast(ptrindicator, NamedIndicator)
                    Populate(nindicator)

                Case IndicatorType_enum.namedvalue
                    cboIndicatorType.Text = "Name Value Indicator"

                    grpNameIndicator.Visible = False
                    grpValueIndicator.Visible = False
                    grpNameValueIndicator.Visible = True
                    grpNameValueIndicator.Location = grpValueIndicator.Location

                    '// list all the little guys
                    Dim nvindicator As NamedValueIndicator = DirectCast(ptrindicator, NamedValueIndicator)
                    Populate(nvindicator)

            End Select

            lblSelectIndicatorType.Visible = False

            cboIndicatorType.Enabled = False

        Catch ex As Exception
            HandleException("frmIndicatorProperties", ex)
        End Try
    End Sub

    Private Sub Populate(ByVal indi As ValueIndicator)
        Try
            numDefaultvalue.Value = indi.defValue
            numMin.Value = indi.Minimum
            numMax.Value = indi.Maximum
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub Populate(ByVal indi As NamedIndicator)
        Try
            lstNames.Items.Clear()

            For Each s As String In DirectCast(ptrindicator, NamedIndicator).names 'indi.names
                lstNames.Items.Add(s)
            Next
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub Populate(ByVal indi As NamedValueIndicator)
        Try
            lvwItems.Items.Clear()

            For Each key As String In indi.Items.Keys
                Dim litem As New ListViewItem(key)
                litem.SubItems.Add(indi.Item(key))
                lvwItems.Items.Add(litem)
            Next
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Public ReadOnly Property Cancelled() As Boolean
        Get
            Return _cancelled
        End Get
    End Property


    Private Function SetValues() As Boolean
        Try

            If ptrindicator.IndicatorType = IndicatorType_enum.value Then
                If numMin.Value > numMax.Value Then
                    MessageBox.Show("Minimum Values must be less than maximum.", "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                    Return False
                End If

                If numDefaultvalue.Value < numMin.Value Then
                    MessageBox.Show("Default Value must be equal or greater than minimum value.", "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                    Return False
                End If

                If numDefaultvalue.Value > numMax.Value Then
                    MessageBox.Show("Default Value must be equal or less than maximum value.", "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                    Return False
                End If

                DirectCast(ptrindicator, ValueIndicator).Maximum = numMax.Value
                DirectCast(ptrindicator, ValueIndicator).Minimum = numMin.Value
                DirectCast(ptrindicator, ValueIndicator).defValue = numDefaultvalue.Value

            Else
                '// its either a name indicator or a name value indicator
                If ptrindicator.IndicatorType = IndicatorType_enum.named Then
                    If DirectCast(ptrindicator, NamedIndicator).names.Count = 0 Then
                        MessageBox.Show("You must have at least one name in this indicator.", "Invalid Indicator", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                        Return False
                    End If

                ElseIf ptrindicator.IndicatorType = IndicatorType_enum.namedvalue Then
                    If DirectCast(ptrindicator, NamedValueIndicator).Items.Count = 0 Then
                        MessageBox.Show("You must have at least one named value in this indicator.", "Invalid Indicator", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                        Return False
                    End If

                End If
            End If

            ptrindicator.IndicatorName = txtName.Text

            Return True

        Catch ex As Exception
            HandleException("frmIndicatorProperties", ex)
            Return False
        End Try
    End Function

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        _cancelled = True
        Me.Hide()
    End Sub

    Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        _cancelled = False
        If SetValues() = True Then
            '// yay!
            Me.Hide()
        End If

    End Sub

    Private Sub cboIndicatorType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboIndicatorType.SelectedIndexChanged
        Try

            '// to prevent overwriting the passed in object
            If bPopulating Then Exit Sub

            Select Case cboIndicatorType.Text
                Case Is = "Value Indicator"
                    ptrindicator = New ValueIndicator
                Case Is = "Name Indicator"
                    ptrindicator = New NamedIndicator
                Case Is = "Name Value Indicator"
                    ptrindicator = New NamedValueIndicator
            End Select

            ptrindicator.IndicatorName = txtName.Text
            btnOK.Enabled = True
            Populate()

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub btnAddName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddName.Click
        Try
            Dim nindicator As NamedIndicator = DirectCast(ptrindicator, NamedIndicator)
            Dim s As String = InputBox("Enter a name", "Enter Name")
            If s.Length > 0 Then

                If Not nindicator.names.Contains(s) Then
                    nindicator.names.Add(s, s)
                Else
                    MessageBox.Show("An entry with that name already exists.", "Invalid Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If

            End If

            Populate(nindicator)

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub btnRemoveName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemoveName.Click
        If lstNames.SelectedItem Is Nothing Then Exit Sub

        Try
            Dim nindicator As NamedIndicator = DirectCast(ptrindicator, NamedIndicator)
            nindicator.names.Remove(lstNames.SelectedItem.ToString)
            lstNames.Items.Remove(lstNames.SelectedItem)
            Populate()
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

    Private Sub btnAddNameValueIndicator_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddNameValueIndicator.Click
        Dim f As New frmNamedValue
        f.ShowDialog()

        Try
            Dim nvindicator As NamedValueIndicator = ptrindicator
            If Not nvindicator.Items.ContainsKey(f.ItemName) Then
                nvindicator.Items.Add(f.ItemName, f.ItemValue)
                Populate(nvindicator)
            Else
                MessageBox.Show("An item with that name already exists.", "Action Cancelled", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub btnRemoveNameValueIndicator_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemoveNameValueIndicator.Click
        If lvwItems.SelectedItems.Count = 0 Then Exit Sub

        Try
            Dim nvindicator As NamedValueIndicator = ptrindicator
            For Each litem As ListViewItem In lvwItems.SelectedItems
                nvindicator.Items.Remove(litem.Text)
                lvwItems.Items.Remove(litem)
            Next
            Populate(nvindicator)

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub
End Class